import 'package:flutter/material.dart';
import 'package:fluintl/fluintl.dart';
import 'package:flutter_fun_app/utils/strings.dart';
import 'package:flutter_common/net/http.dart';
import 'package:flutter_fun_app/model/newsModel.dart';
import 'package:flutter_common/widget/loadingAnim.dart';
import 'package:flutter_common/widget/funAppBar.dart';
import 'package:fluintl/fluintl.dart';
import 'package:flutter_fun_app/utils/strings.dart';

class NewsPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return NewsState();
  }
}

class NewsState extends State<NewsPage> with SingleTickerProviderStateMixin {
  TabController _tabController;
  List<String> tabTitleList = List();
  Data modelData;
  var titlesIds = [
    Ids.news_title_auto,
    Ids.news_title_dy,
    Ids.news_title_ent,
    Ids.news_title_money,
    Ids.news_title_sport,
    Ids.news_title_tech,
    Ids.news_title_toutiao,
    Ids.news_title_war
  ];

  void initState() {
    super.initState();
    getNewsData();
    _tabController = new TabController(vsync: this, length: titlesIds.length);
  }

  Future<void> getNewsData() async {
    var url = "https://www.apiopen.top/journalismApi";
    HttpUtil.get(url, success: (data) {
      NewsModel newsModel = NewsModel.fromJson(data);
      if (newsModel.code == 200) {
        setState(() {
          modelData = newsModel.data;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: FunAppBar(
        title: IntlUtil.getString(context, Ids.title_radio),
        bottom: TabBar(
          tabs: titlesIds
              .map((e) => Tab(text: IntlUtil.getString(context, e)))
              .toList(),
          isScrollable: true,
          indicatorSize: TabBarIndicatorSize.tab,
          controller: _tabController,
        ),
      ),
      body: modelData == null
          ? LoadingAnim.getProgressAnim()
          : TabBarView(
              controller: _tabController,
              children: <Widget>[
                NewContentPage(modelData.auto),
                NewContentPage(modelData.dy),
                NewContentPage(modelData.ent),
                NewContentPage(modelData.money),
                NewContentPage(modelData.sports),
                NewContentPage(modelData.tech),
                NewContentPage(modelData.toutiao),
                NewContentPage(modelData.war),
              ],
            ),
    );
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }
}

class NewContentPage extends StatefulWidget {
  List<Auto> datas;

  NewContentPage(this.datas);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return NewContentState();
  }
}

class NewContentState extends State<NewContentPage> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ListView.builder(
        itemCount: widget.datas.length,
        itemBuilder: (BuildContext context, int index) {
          return itemBuilder(index);
        });
  }

  Widget itemBuilder(int index) {
    Auto item = widget.datas[index];
    if (item.title == null) {
      return null;
    }
    return Column(
      children: <Widget>[
        ListTile(
          title: Text(
            item.title,
            style: TextStyle(color: Colors.black, fontSize: 16),
          ),
          subtitle: Text(
            item.digest,
            style: TextStyle(color: Colors.grey, fontSize: 13),
          ),
          onTap: () {},
        ),
        Divider(
          height: 2,
        )
      ],
    );
  }
}
